<template>
  <!-- 整个界面边框 -->
  <dv-border-box-11 class="dashboard-container">
    <div class="title">农业数据可视化大屏</div>
    <el-container class="dark-theme">
      <!-- 左侧模块 -->
      <el-aside width="300px" class="left-aside">
        <!-- 大棚信息 -->
        <dv-border-box-1 class="greenhouse-info">
          <el-card shadow="hover">
            <h3>大棚信息</h3>
            <div class="info-content">
              <p>大棚编号: {{ greenhouseInfo.id }}</p>
              <p>大棚位置: {{ greenhouseInfo.location }}</p>
              <p>大棚面积: {{ greenhouseInfo.area }} ㎡</p>
            </div>
          </el-card>
        </dv-border-box-1>

        <!-- 土壤信息 -->
        <el-row :gutter="20" class="soil-info">
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤PH值</h3>
                <div class="value">{{ sensorData.soilPh }}</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤含水率</h3>
                <div class="value">{{ sensorData.soilMoisture }}%</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="soil-info">
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤温度</h3>
                <div class="value">{{ sensorData.soilTemperature }}°C</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤电导率</h3>
                <div class="value">{{ sensorData.soilConductivity }} µS/cm</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>

        <!-- 空气信息 -->
        <el-row :gutter="20" class="air-info">
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>空气温度</h3>
                <gauge-chart :value="sensorData.airTemperature" :max="50" :min="-10"></gauge-chart>
              </el-card>
            </dv-border-box-1>
          </el-col>
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>空气湿度</h3>
                <gauge-chart :value="sensorData.airHumidity" :max="100" :min="0"></gauge-chart>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>
      </el-aside>

      <!-- 中间模块 -->
      <el-main class="main-content">
        <!-- 摄像头实时预览 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>摄像头实时预览</h3>
                <div id="ezuikit-player"></div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>

        <!-- 摄像头历史截图 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>摄像头历史截图</h3>
                <div class="history-snapshot">
                  <img :src="cameraImageUrl" alt="历史截图" />
                </div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>
      </el-main>

      <!-- 右侧模块 -->
      <el-aside width="300px" class="right-aside">
        <!-- 土壤氮、磷、钾含量 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤氮含量</h3>
                <line-chart :data="soilNitrogenData"></line-chart>
              </el-card>
            </dv-border-box-1>
          </el-col>
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤磷含量</h3>
                <line-chart :data="soilPhosphorusData"></line-chart>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>土壤钾含量</h3>
                <line-chart :data="soilPotassiumData"></line-chart>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>

        <!-- 天气预览 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>天气预览</h3>
                <div class="weather-preview">
                  <img src="https://example.com/weather.png" alt="天气预览" />
                </div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>

        <!-- 光照度和二氧化碳 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>光照度</h3>
                <div class="value">{{ sensorData.lightIntensity }} Lux</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
          <el-col :span="12">
            <dv-border-box-1>
              <el-card shadow="hover">
                <h3>二氧化碳</h3>
                <div class="value">{{ sensorData.co2 }} ppm</div>
              </el-card>
            </dv-border-box-1>
          </el-col>
        </el-row>
      </el-aside>
    </el-container>
  </dv-border-box-11>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { BorderBox11 as DvBorderBox11, BorderBox1 as DvBorderBox1 } from '@kjgl77/datav-vue3';
import axios from 'axios';

// 数据定义
const greenhouseInfo = ref({
  id: '001',
  location: '北京市朝阳区',
  area: 500,
});

const sensorData = ref({
  soilPh: 6.5,
  soilMoisture: 45,
  soilTemperature: 25,
  soilConductivity: 1200,
  airTemperature: 28,
  airHumidity: 60,
  lightIntensity: 1200,
  co2: 400,
});

const soilNitrogenData = ref([]);
const soilPhosphorusData = ref([]);
const soilPotassiumData = ref([]);
const cameraImageUrl = ref('');

import { getGreenhouseInfo, getSensorData, getSoilNitrogenData, getCameraImageUrl } from '@/api/greenhouse';

// 获取大棚信息
const fetchGreenhouseInfo = async () => {
  try {
    const response = await getGreenhouseInfo(1); // 假设大棚ID为1
    greenhouseInfo.value = response;
  } catch (error) {
    console.error('Failed to fetch greenhouse info:', error);
  }
};

// 获取传感器数据
const fetchSensorData = async () => {
  try {
    const response = await getSensorData();
    sensorData.value = response;
  } catch (error) {
    console.error('Failed to fetch sensor data:', error);
  }
};

// 获取土壤氮含量数据
const fetchSoilNitrogenData = async () => {
  try {
    // 获取今天的日期
    const today = new Date().toISOString().split('T')[0]; // 格式为 yyyy-MM-dd

    // 请求参数
    const params = {
      date: today, // 今天的日期
      sensorName: '土壤氮含量' // 传感器名称
    };

    // 发送请求
    const response = await getSoilNitrogenData(params);
    soilNitrogenData.value = response; // 更新图表数据
  } catch (error) {
    console.error('Failed to fetch soil nitrogen data:', error);
  }
};

// 获取摄像头图片URL
const fetchCameraImageUrl = async () => {
  try {
    const response = await getCameraImageUrl('your-device-serial'); // 替换为实际的设备序列号
    cameraImageUrl.value = response;
  } catch (error) {
    console.error('Failed to fetch camera image URL:', error);
  }
};

// 初始化摄像头预览
const initCameraPreview = () => {
  const player = new EZUIKit.EZUIKitPlayer({
    id: 'ezuikit-player', // 播放器容器的ID
    url: 'ezopen://open.ys7.com/AB9831171/1.hd.live', // 监控视频的实时预览地址
    accessToken: 'ra.5re72ai38g4y7gky9ghls5dzb2equqku-491d6ysuja-0bzlevz-5zrd54nns', // 萤石云的访问Token
    template: 'simple', // 界面模板，可选 simple 或 standard
  });
};

onMounted(() => {
  fetchGreenhouseInfo();
  fetchSensorData();
  fetchSoilNitrogenData();
  fetchCameraImageUrl();
  initCameraPreview();
});
</script>

<style scoped>
.dashboard-container {
  width: 100vw;
  height: 100vh;
  padding: 20px;
  background-color: #1e1e1e;
  overflow: hidden; /* 防止滚动条 */
}

.title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  color: #ffffff;
  margin-bottom: 20px;
}

.dark-theme {
  background-color: #2d2d2d;
  color: #ffffff;
  height: calc(100vh - 60px); /* 减去标题高度 */
  display: flex;
}

.left-aside, .right-aside {
  height: 100%;
  overflow: hidden; /* 防止滚动条 */
}

.main-content {
  flex: 1;
  height: 100%;
  overflow: hidden; /* 防止滚动条 */
}

.el-card {
  background-color: #3a3a3a;
  color: #ffffff;
  margin-bottom: 20px;
  border-radius: 8px;
  height: 100%;
}

.value {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  color: #ffffff;
}

#ezuikit-player {
  width: 100%;
  height: 300px; /* 调整视频高度 */
}

.history-snapshot img {
  width: 100%;
  height: auto;
}

.weather-preview img {
  width: 100%;
  height: auto;
}
</style>